package cn.djun.news.service.impl;

import cn.djun.news.dao.CollectDao;
import cn.djun.news.entity.Collect;
import cn.djun.news.service.CollectService;
import cn.djun.news.util.Paginating;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author djun
 * @classname CollectServiceImpl
 * @description
 * @date 2020/12/3 9:19
 * @email 3170495286@qq.com
 */
@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectDao collectDao;

    /**
     * 添加收藏
     *
     * @param collect
     * @return
     */
    @Override
    public int addCollect(Collect collect) {
        try {
            return collectDao.insertNews(collect);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据userid查询收藏
     *
     * @param collect
     * @return
     */
    @Override
    public Paginating<Collect> getCollectlist(@RequestBody Collect collect) {
        try {
            PageHelper.startPage(collect.getPageNum(), collect.getPageSize());
            List<Collect> list = collectDao.getCollectList(collect);
            PageInfo<Collect> p = new PageInfo<>(list);
            return new Paginating<>(p);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据userid取消收藏（删除）
     *
     * @param collect
     * @return
     */
    @Override
    public int delCollect(Collect collect) {
        try {
            return collectDao.delCollect(collect);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

}
